package com.zxj.schoolxianyu.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zxj.schoolxianyu.dao.collectDao;
import com.zxj.schoolxianyu.dao.goodsDao;
import com.zxj.schoolxianyu.entity.collect;
import com.zxj.schoolxianyu.entity.goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class getCollectedGoodsService {
    @Autowired(required = false)
    private collectDao collectDao;
    @Autowired(required = false)
    private goodsDao goodsDao;

    public String getCollectedGoods(String username) {
        EntityWrapper<collect> c = new EntityWrapper<>();
        EntityWrapper<goods> g = new EntityWrapper<>();
        c.eq("username", username);
        List<collect> cs = collectDao.selectList(c);
        List<Integer> goodsids = new ArrayList<Integer>();
        for (int i = 0; i < cs.size(); i++) {
            goodsids.add(cs.get(i).goodsid);
        }
        String value = goodsids.toString().replace("[", "(");
        value = value.replace("]", ")");
        g.in("goodsid", value);
        List<goods> gs = goodsDao.selectList(g);
        if (gs.isEmpty()) return "No more data";
        else return JSON.toJSONString(gs);
    }
}
